<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      
        <!-- <el-form-item label="姓名" prop="lPillarId">
          <el-input
            v-model="queryParams.lPillarId"
            placeholder="请输入性"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->

        <el-form-item label="姓名" prop="lPillarId" >
            <el-select v-model="queryParams.lPillarId" filterable placeholder="请选择">
            <el-option
              v-for="item in friendsList"
              :key="item.lUserId"
              :label="item.lRealName"
              :value="item.lUserId">
            </el-option>
          </el-select>
          </el-form-item>
        <el-form-item label="事件类型" prop="lEventType">
          <!-- <el-input
            v-model="queryParams.lEventType"
            placeholder="请输入事件类型"
            clearable
            @keyup.enter.native="handleQuery"
          /> -->
          <el-select
              v-model="queryParams.lEventType"
              placeholder="请输入事件类型"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.life_event_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="礼尚往来" prop="lGiftWay">
          <!-- <el-input
            v-model="queryParams.lGiftWay"
            placeholder="请输入礼物方式"
            clearable
            @keyup.enter.native="handleQuery"
          /> -->
          <el-select
              v-model="queryParams.lGiftWay"
              placeholder="请输入"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.life_gift_way"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="礼金" prop="lCashGift">
          <el-input
            v-model="queryParams.lCashGift"
            placeholder="请输入礼金"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="礼物" prop="lGift">
          <el-input
            v-model="queryParams.lGift"
            placeholder="请输入礼物"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="queryParams.remark"
            placeholder="请输入备注"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-tag id="tagMoney" type="danger">￥{{totalMoney}}</el-tag>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['life:list:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['life:list:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['life:list:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['life:list:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="listList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
       

        <el-table-column prop="lGiftWay" label="礼尚往来" width="150" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.life_gift_way" :value="scope.row.lGiftWay"/>
          </template>
        </el-table-column>

        <el-table-column prop="lEventType" label="事件类型" width="150" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.life_event_type" :value="scope.row.lEventType"/>
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="lFamilyFriends.lRealName" />
        <el-table-column label="礼金" align="center" prop="lCashGift" />
        <el-table-column label="礼物" align="center" prop="lGift" />
        <el-table-column label="备注" align="center" prop="remark" />

        <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['life:list:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['life:list:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改人情世故对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="礼尚往来">
          <el-select v-model="form.lGiftWay" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.life_gift_way"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="事件类型">
          <el-select v-model="form.lEventType" placeholder="请选择事件类型">
            <el-option
              v-for="dict in dict.type.life_event_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="主人id" prop="lPillarId">
            <el-input v-model="form.lPillarId" placeholder="请输入主人id" />
          </el-form-item> -->

        
          <el-form-item label="姓名" >
            <el-select v-model="form.lPillarId" filterable placeholder="请选择">
            <el-option
              v-for="item in friendsList"
              :key="item.lUserId"
              :label="item.lRealName"
              :value="item.lUserId">
            </el-option>
          </el-select>
          </el-form-item>
      
          <el-form-item label="礼金" prop="lCashGift">
            <el-input v-model="form.lCashGift" placeholder="请输入礼金" />
          </el-form-item>
          <el-form-item label="礼物" prop="lGift">
            <el-input v-model="form.lGift" type="textarea" placeholder="请输入礼物" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listList, getList, delList, addList, updateList } from "@/api/life/gift";
  import { listFriends } from "@/api/life/friends";
  
  export default {
    name: "List",
    dicts: ['life_gift_way','life_event_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 人情世故表格数据
        listList: [],
        //人员总条数
        friendTotal:0,
        //人员列表
        friendsList:[],

        floading: true,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          lEventType: null,
          lPillarId: null,
          lGiftWay: null,
          lCashGift: null,
          lGift: null,
          lGiftDescription: null,
        },
        userParams: {
            pageNum: 1,
            pageSize: 10000,
            lRealName: null,
        },
        // 表单参数
        form: {
          lGiftId: null,
          lEventType: null,
          lPillarId: null,
          lGiftWay: null,
          lCashGift: null,
          lGift: null,
          lGiftDescription: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          lFamilyFriends:{
            lUserId: null,
            lRealName: null,
          }
         
        },
        // 表单校验
        rules: {
          lEventType: [
            { required: true, message: "事件类型不能为空", trigger: "blur" }
          ],
          lPillarId: [
            { required: true, message: "主场不能为空", trigger: "blur" }
          ],
          lGiftWay: [
            { required: true, message: "礼物方式不能为空", trigger: "blur" }
          ],
          
        },
        totalMoney:0.00,
      
      };
    },
    created() {
      this.getList();
      this.getUserList();
    },
    methods: {
        /** 查询亲戚朋友列表 */
        getUserList() {
          this.floading = true;
          listFriends(this.userParams).then(response => {
            this.friendsList = response.rows;
            this.friendTotal = response.total;
            this.floading = false;
          });
        },

      /** 查询人情世故列表 */
      getList() {
        this.loading = true;
        listList(this.queryParams).then(response => {
          this.listList = response.rows;
          this.total = response.total;
          this.loading = false;
          if(this.listList.length==0){
            this.totalMoney = 0.00
          }else{
            this.totalMoney = this.listList[0].totalMoney;
          }
          
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          lGiftId: null,
          lEventType: null,
          lPillarId: null,
          lGiftWay: null,
          lCashGift: null,
          lGift: null,
          lGiftDescription: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          lFamilyFriends:{
            lUserId: null,
            lRealName: null,
          }
         
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.lGiftId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加人情世故";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const lGiftId = row.lGiftId || this.ids
        getList(lGiftId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改人情世故";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.lGiftId != null) {
              updateList(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addList(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const lGiftIds = row.lGiftId || this.ids;
        this.$modal.confirm('是否确认删除人情世故编号为"' + lGiftIds + '"的数据项？').then(function() {
          return delList(lGiftIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('life/list/export', {
          ...this.queryParams
        }, `list_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
<style>
#tagMoney{
  margin-left: 20px;
  font-size: large;
}

</style>